//incontent.js

var like = randomize();
var islike = 0;
var isdislike = 0;

function createElements()
{
	var addto = document.getElementById("container");
	var newlike;var newbuttonlike;var newbuttondislike;var imglike;
	
	newlike = document.createElement('span');
	newbuttonlike = document.createElement('button');
	newbuttondislike = document.createElement('button');
	imglike = document.createElement('img');
	
	newlike.innerHTML= /* "TotalLike: "+ */like;
	newlike.setAttribute('id',"likein");
	
	imglike.setAttribute('src','theme/like-mark.png');
	imglike.setAttribute('class','thumbupin');
	
	newbuttonlike.innerHTML = "Like";
	newbuttonlike.setAttribute('id',"but_like");
	newbuttonlike.setAttribute('onclick','redrawLikedElement(this)');
	newbuttonlike.setAttribute('class','but_likein');
	
	newbuttondislike.innerHTML = "Dislike";
	newbuttondislike.setAttribute('id',"but_dislike");
	newbuttondislike.setAttribute('onclick','redrawDislikedElement(this)');
	newbuttondislike.setAttribute('class','but_dislikein');
	
	addto.appendChild(imglike);
	addto.appendChild(newlike);
	addto.appendChild(newbuttonlike);
	addto.appendChild(newbuttondislike);
}
function randomize()
{
	var randVal = -50000+(Math.random()*100000);
	return Math.round(randVal);
}

function redrawDislikedElement(button)
{
	var oldspan = document.getElementById('likein');
	if(isdislike == 0)
	{
		like--;
		oldspan.innerHTML = /* "TotalLike: " +l */like;
		islike=0;
		isdislike++;
	}
}
function redrawLikedElement(button)
{
	var oldspan = document.getElementById('likein');
	if(islike == 0)
	{
		like++;
		oldspan.innerHTML =/*  "TotalLike: " +  */like;
		islike++;
		isdislike=0;
	}
}
function addComment()
{
	var currenttime = new Date();
	var date = currenttime.getDay()+"/"+(currenttime.getMonth()+1)+"/"+currenttime.getFullYear();
	var time = currenttime.getHours()+":"+currenttime.getMinutes();
	var thistime = "commented on: "+date+"  "+time;
	
	//alert(thistime);
	var textarea = document.getElementById('boxcomment');
	var buttondislike = document.getElementById('but_dislike');
	var newcomment = document.createElement('div');
	newcomment.setAttribute('class','commentwrapper');
/* 	newcomment.setAttribute('width','300px');
	newcomment.setAttribute('height','50px'); */
	//alert(textarea.value);
	if(textarea.value.length <= 80)
	{
		newcomment.innerHTML ="<div class=time>"+thistime+"</div>"+"<div class=comment>"+textarea.value+"</div>";
	}
	else
	{
		var valuetext = ""+textarea.value;
		newcomment.innerHTML ="<div class=time>"+thistime+"</div>"+"<div class=comment>"+textarea.value.substring(0,80)+" "+"<span class=readmore onclick=resizecommentbox(this,"+ textarea.value.length +",\""+ valuetext+"\")>Read More...</span>"+"</div>";
	}
	textarea.value = "";
	textarea.parentNode.insertBefore(newcomment,buttondislike.nextSibling);
}

function resizecommentbox(box1,size,teks)
{
	var countrow =  Math.ceil(size/80);
	var newheight= 50+countrow*17;
	//alert(50+countrow*17);
	for(i=1;i<countrow+1;i++)
	{
		// alert(teks.substr(0,i*83)); 
		teks = teks.substr(0,i*80)+"</br>"+teks.substr((i*80)+2);
	}
	box1.parentNode.parentNode.setAttribute('style',"height:"+newheight+"px");
	box1.parentNode.innerHTML ="<p>"+teks+"</p>";
	//box.innerHTML = text;
}